package com.chinaunicom.product.trigger.rpc;

import com.chinaunicom.api.IProductFacadeApi;
import com.chinaunicom.api.dto.ProductSkuDTO;
import com.chinaunicom.api.dto.ProductSkuQuery;
import com.chinaunicom.common.exception.BusinessException;
import com.chinaunicom.common.result.JsonResult;
import com.chinaunicom.product.domain.model.entity.ProductSkuEntity;
import com.chinaunicom.product.domain.repository.IProductRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author guomenghuai
 * @date 2024/1/10
 * @Description
 */
@Service
public class ProductFacadeApi implements IProductFacadeApi {

    @Resource
    private IProductRepository productRepository;

    @Override
    public JsonResult<List<ProductSkuDTO>> listProductSku(ProductSkuQuery productSkuQuery) {
        if (Objects.isNull(productSkuQuery) || CollectionUtils.isEmpty(productSkuQuery.getSkuCodeList())) {
            throw new BusinessException("商品编码为空");
        }
        List<ProductSkuEntity> productEntityList = productRepository.queryListProductSkuByCode(productSkuQuery.getSkuCodeList(), productSkuQuery.getSellerId());

        List<ProductSkuDTO> productList = productEntityList.stream().map(item -> {
            ProductSkuDTO productSkuDTO = new ProductSkuDTO();
            BeanUtils.copyProperties(item, productSkuDTO);
            return productSkuDTO;
        }).collect(Collectors.toList());
        return JsonResult.success(productList);
    }
}
